package com.voute.View.Activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.umeng.analytics.MobclickAgent;
import com.voute.App.App;
import com.voute.Base.BaseActivity;
import com.voute.Model.Bean.LoginInfoBean;
import com.voute.Presenter.ILoginPresenter;
import com.voute.Presenter.Impl.LoginPresenterImpl;
import com.voute.R;
import com.voute.Utils.PreferenceUtils;
import com.voute.View.IView.ILoginView;

/**
 * 登录画面
 * Created by liucxu on 2017/1/17.
 */

public class LoginActivity extends BaseActivity implements ILoginView {
    private RelativeLayout back_layout;
    private TextView register_tv;
    private EditText login_phone_et;
    private EditText register_user_password_et;
    private TextView login_tv;
    private MaterialDialog progressDialog;
    private ILoginPresenter presenter;
    private TextView tv_forget_password;


    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void setupView() {
        back_layout = $(R.id.back_layout);
        register_tv = $(R.id.register_tv);
        login_phone_et = $(R.id.login_phone_et);
        tv_forget_password = $(R.id.tv_forget_password);
        register_user_password_et = $(R.id.register_user_password_et);
        login_tv = $(R.id.login_tv);
        presenter = new LoginPresenterImpl(this);
        back_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                App.GetInstance().finishActivity(LoginActivity.this);
            }
        });
        register_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                LoginActivity.this.startActivity(intent);
            }
        });

        tv_forget_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, ForgetPasswordActivity.class);
                LoginActivity.this.startActivity(intent);
            }
        });
        login_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkValidate()) {
                    presenter.login(login_phone_et.getText().toString(), register_user_password_et.getText().toString());
                }
            }
        });
    }

    @Override
    public void setupData() {

    }

    @Override
    public boolean checkValidate() {
        if (TextUtils.isEmpty(login_phone_et.getText().toString())) {
            showError("请输入手机号码");
            return false;
        }
        if (TextUtils.isEmpty(register_user_password_et.getText().toString())) {
            showError("请输入密码");
            return false;
        }
        return true;
    }

    @Override
    public void loginSuccess(LoginInfoBean info) {
        MobclickAgent.onEvent(LoginActivity.this, " login_finish");
        PreferenceUtils.SaveUserToken(info.getData().getToken());
//        Intent intent = new Intent(this, MainActivity.class);
//        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        startActivity(intent);
        PreferenceUtils.SaveUserIdHash(info.getData().getHash());
        App.GetInstance().finishClassActivity(RegisterActivity.class);
        App.GetInstance().finishClassActivity(RegisterActivity.class);
        App.GetInstance().finishActivity(this);
    }

    @Override
    public void showLoading() {
        if (progressDialog == null) {
            progressDialog = new MaterialDialog.Builder(this)
                    .progress(true, 0)
                    .title("提示")
                    .content("请等待...")
                    .autoDismiss(false)
                    .cancelable(true)
                    .build();
        }
        progressDialog.show();
    }

    @Override
    public void hideLoading() {
        progressDialog.hide();
    }

    @Override
    public void showError(String errMsg) {
//        Snackbar.make(login_tv, errMsg, Snackbar.LENGTH_SHORT).show();
        MaterialDialog notice_dialog = new MaterialDialog.Builder(LoginActivity.this).title("提示").positiveText("确认").cancelable(false).content(errMsg).build();
        notice_dialog.show();
    }
}
